<!--JS脚本部分，不要将JS脚本写在其它地方-->
<script type="text/javascript" src="/linksforbt/static/js/vue.min.js"></script>
<script type="text/javascript" src="/linksforbt/static/js/axios.min.js"></script>

<style>
    /*样式写这里*/
    
    .demo-table table tbody tr td span {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 580px;
        display: block;
    }
    
    .call_me {
        margin-top: 10px;
    }
    
    .line .tname {
        width: 150px;
    }
</style>
<div id="linksforbt_app" class="bt-form">
    <div class="bt-w-main">
        <!--菜单部分-->
        <div class="bt-w-menu">
            <p :class="app_tab==1?'bgw':''" @click="app_tab=1;">操作</p>
            
            <p :class="app_tab==10?'bgw':''" @click="app_tab=10;">关于</p>
        </div>
        <!--内容部分-->
        <div class="bt-w-con pd15">
            <div class="plugin_body">
              
                <div class="soft-man-con bt-form" v-show="app_tab==1" >
                <form @submit.prevent="submit">
                    <div class="line"><span class="tname">选择源目录</span>
                        <div class="info-r">

                            <input name="links_source_folder" class=" bt-input-text mr5 links_source" ref='links_source'  :placeholder="links.source?links.source:'/www/wwwroot'" v-model="links.source" required="required" readonly>
                            <span data-id="links_source_folder" class="glyphicon cursor mr5 glyphicon-folder-open" @click="select_path('links_source');"></span>
                            <span style="color: rgb(153, 153, 153);">必须是绝对路径</span>
                        </div>
                    </div>
                    <div class="line"><span class="tname">目标</span>
                        <div class="info-r">
                            <input name="links_target_folder" class=" bt-input-text mr5 links_target" ref='links_target' :placeholder="'/www/wwwroot'" v-model="links.target" required="required" readonly>
                            <span data-id="links_target_folder" class="glyphicon cursor mr5 glyphicon-folder-open" @click="select_path('links_target')"></span>
                            <span style="color: rgb(153, 153, 153);">必须是绝对路径</span>
                        </div>
                    </div>
                    <div class="line"><span class="tname">目标文件夹名称</span>
                        <div class="info-r">
                          <input  placeholder='请填写目标文件夹名称' type="text" class="bt-input-text mr5"  v-model="links.target_folder">
                            <span style="color: rgb(153, 153, 153);">建议填写</span>  
                        </div>
                    </div>
                    <div class="line"><span class="tname"></span>
                        <div class="info-r"><button class="btn btn-success btn-sm"  >创建软连接</button>
                        </div>
                    </div>
    </form>  
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>源目录</th>
                                <th>目标文件夹</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(rely, index) in link_list" :key="index">
                                <td>{{rely.source}}</td>
                                <td>{{rely.target_links}}
                                </td>
                                <td><a href="#" @click="del_links(rely)">删除软连接</a></td>

                            </tr>

                        </tbody>
                    </table>

                </div>

                <div class="bt-box about" v-show="app_tab==10" style="display: none;">
                    <p class="status mb15">如果您在使用上有任何问题或建议，请您反馈到以下链接：</p>
                    <p class="status mb15"><a :href="abouts.issues_link" target="_blank" rel="noreferrer">{{abouts.issues_link}}</a></p>
                    <p class="status mb15">未来功能构想：</p>
                    <table class="table table-hover">

                        <tbody>
                            <tr v-for="(rely, index) in abouts.new_ideas" :key="index">
                                <td>{{rely}}</td>
                            </tr>

                        </tbody>
                    </table>

                    <p class="status">本项目主要依赖以下项目或组件</p>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>名称</th>
                                <th>网站</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(rely, index) in abouts.rely_on" :key="index">
                                <td>{{rely.title}}</td>
                                <td><a :href="rely.link" target="_blank" rel="noreferrer">{{rely.link}}</a>
                                </td>
                            </tr>

                        </tbody>
                    </table>
                </div>


                <p class="call_me">联系方式：</p>
                <p v-for="(contact, index) in abouts.contact_me" :key="index" class="">
                    <span>{{index}}</span>:<span style="color: #20a53a; margin-left: 3px;">{{contact}}</span>
                </p>

            </div>
        </div>
    </div>
</div>

<script>
    $(function() {
        let wx = new Vue({
            el: '#linksforbt_app',
            data: {
                app_tab: 1,
                logs: '',
                links:{
                  source:'',
                  target:'',
                  target_folder:''
                },
                link_list:{},
                abouts: {
                    issues_link: '',
                    contact_me: {
                        qq: '747357766',
                    },
                    new_ideas: [
                        '1.',
                    ],
                    rely_on: [{
                        'title': '宝塔面板',
                        'link': 'https://www.bt.cn/'
                    },{
                        'title': 'Vue.js',
                        'link': 'https://cn.vuejs.org/'
                    }, {
                        'title': 'axios',
                        'link': 'https://github.com/axios/axios'
                    }, {
                        'title': 'Python',
                        'link': 'https://www.python.org/'
                    }, {
                        'title': 'layer',
                        'link': 'https://layer.layui.com/'
                    },{
                        'title': 'linksforbt 开源地址',
                        'link': 'https://gitee.com/liushuai05/linksforbt.git'
                    }, ]
                }
            },
            created() {
                axios.defaults.headers.common = jQuery.ajaxSettings.headers;

                this.get_status()
            },
           

            methods: {
               submit (){
                 let loadin = layer.load();
                  let that = this;
                  let timeout = 3600;
                 if(that.links.source==''){
                   layer.msg('请选择源目录!', {
                        icon: 2
                    });
                    layer.close(loadin);
                  return false;
                 }
                if(that.links.target==''){
                   layer.msg('请选择目标目录!', {
                        icon: 2
                    });
                    layer.close(loadin);
                  return false;
                 }

                 let data = {
                        source: that.links.source,
                        target: that.links.target,
                        target_folder: that.links.target_folder,
                        callback: 'linksforbt.add_links'
                    };
                    // layer.msg('请求过程发现错误!', { icon: 2 });

                  $.ajax({
                        type: 'POST',
                        url: '/plugin?action=a&s=add_links&name=linksforbt',
                        dataType: 'json',
                        data: data,
                        timeout: timeout,
                        success: function (rdata) {
                            layer.close(loadin);
                            if(rdata.status==0){
                                console.log(rdata)
                                layer.msg(rdata.message, { icon: 2 });
                                return false;
                            }
				                    that.link_list=rdata.link_list
                            // that.set_status('reload', 1);
                            // layer.msg('操作成功!', { icon: 1 });
                        },
                        error: function (ex) {
                            layer.msg('请求过程发现错误!', { icon: 2 });
                        }
                    });
              },
            
              get_status() {
                    let loadin = layer.load();
                    let that = this;
                    let timeout = 3600;
                    let data = {
                        status: status,
                        callback: 'linksforbt.get_status'
                    }

                    $.ajax({
                        type: 'POST',
                        url: '/plugin?action=a&s=get_status&name=linksforbt',
                        dataType: 'json',
                        data: data,
                        timeout: timeout,
                        success: function (rdata) {
                            layer.close(loadin);
                            if(rdata.status==0){
                                console.log(rdata)
                                layer.msg(rdata.message, { icon: 2 });
                            }
                            that.link_list=rdata.link_list
                            
                            // that.status = rdata.data.status
                            // that.logs = rdata.data.run_logs

                            console.log(rdata)
                            layer.close(loadin);
                            console.log(rdata.data);

                            // that.set_status('reload', 1);
                            // layer.msg('操作成功!', { icon: 1 });
                        },
                        error: function (ex) {
                            layer.msg('请求过程发现错误!', { icon: 2 });
                        }
                    });


                },
                del_links(item){
                    let loadin = layer.load();
                    let that = this;
                    let timeout = 3600;
                    console.log(item.target_links)

                    let data = {
                        target_links: item.target_links,
                        callback: 'linksforbt.del_links'
                    };
                    // layer.msg('请求过程发现错误!', { icon: 2 });

                  $.ajax({
                        type: 'POST',
                        url: '/plugin?action=a&s=del_links&name=linksforbt',
                        dataType: 'json',
                        data: data,
                        timeout: timeout,
                        success: function (rdata) {
                            layer.close(loadin);
                            if(rdata.status==0){
                                console.log(rdata)
                                layer.msg(rdata.message, { icon: 2 });
                            }
				            that.link_list=rdata.link_list
                            // that.set_status('reload', 1);
                            // layer.msg('操作成功!', { icon: 1 });
                        },
                        error: function (ex) {
                            layer.msg('请求过程发现错误!', { icon: 2 });
                        }
                    });

                },
                // 清理运行日志
                clear_runlog() {
                    let that = this;
                    let timeout = 3600;
                    let data = {
                        status: status,
                        callback: 'linksforbt.clear_runlog'
                    };
                    layer.confirm('你确定要清理运行日志?', {
                        btn: ['确定', '取消'] //按钮
                    }, function() {
                        layer.closeAll('dialog');

                        axios.post(`/plugin?action=a&s=clear_runlog&name=linksforbt`, data, {
                                timeout: timeout
                            })
                            .then(function(response) {
                                if (response.data.status == 1) {
                                    layer.msg('操作成功!', {
                                        icon: 1
                                    });
                                    that.get_status()
                                } else {
                                    layer.msg('请求过程发现错误!', {
                                        icon: 1
                                    });
                                }
                                console.log(response.data);
                            })
                            .catch(function(error) {
                                console.log(error);
                                layer.msg('请求过程发现错误!', {
                                    icon: 2
                                });
                            });
                    });
                },
                set_status(status, silence = 0) {
                    let that = this;
                    let status_arr = {
                        'stop': '停止运行',
                        'restart': '重启服务',
                        'reload': '重载配置',
                        'start': '启动服务',
                    }
                    status_str = status_arr[status];

                    var fun_setstatus = function(status, silence) {

                        let route = {
                            plugin: 'linksforbt',
                            model: 'set_status'
                        }
                        let data = {
                            status: status
                        };

                        $.ajax({
                            type: 'POST',
                            url: `/plugin?action=a&s=${route.model}&name=${route.plugin}`,
                            dataType: 'json',
                            data: data,
                            success: function(rdata) {
                                if (silence == 0) {
                                    setTimeout(() => {
                                        that.get_status()
                                    }, 500);
                                    layer.msg('操作成功!', {
                                        icon: 1
                                    });
                                }
                            },
                            error: function(ex) {
                                layer.msg('请求过程发现错误!', {
                                    icon: 2
                                });
                            }
                        });
                    }

                    if (silence == 0) {
                        layer.confirm(`你确定要${status_str}?`, {
                            btn: ['确定', '取消'] //按钮
                        }, function() {
                            layer.closeAll('dialog');
                            fun_setstatus(status, silence)
                        });
                    } else {
                        fun_setstatus(status, silence)
                    }

                },
                  set_cookie : function(key,val)
              {
                var Days = 30;
                var exp = new Date();
                exp.setTime(exp.getTime() + Days*24*60*60*1000);
                document.cookie = key + "="+ escape (val) + ";expires=" + exp.toGMTString();
              },
              get_cookie : function(key)
              {
                var arr,reg=new RegExp("(^| )"+key+"=([^;]*)(;|$)");
                if(arr=document.cookie.match(reg))
                {
                  var val = unescape(arr[2]);
                  return val== 'undefined'?'':val;
                }
                else{
                  return null;
                }
              },
              select_path:function(id){
                  _this = this;
                  _this.set_cookie("SetName", "");
                  var loadT = bt.open({
                    type: 1,
                    area: "650px",
                    title: lan.bt.dir,
                    closeBtn: 2,
                    shift: 5,
                    content: "<div class='changepath'><div class='path-top'><button type='button' id='btn_back' class='btn btn-default btn-sm'><span class='glyphicon glyphicon-share-alt'></span> "+lan.public.return+"</button><div class='place' id='PathPlace'>"+lan.bt.path+"：<span></span></div></div><div class='path-con'><div class='path-con-left'><dl><dt id='changecomlist' >"+lan.bt.comp+"</dt></dl></div><div class='path-con-right'><ul class='default' id='computerDefautl'></ul><div class='file-list divtable'><table class='table table-hover' style='border:0 none'><thead><tr class='file-list-head'><th width='40%'>"+lan.bt.filename+"</th><th width='20%'>"+lan.bt.etime+"</th><th width='10%'>"+lan.bt.access+"</th><th width='10%'>"+lan.bt.own+"</th><th width='10%'></th></tr></thead><tbody id='tbody' class='list-list'></tbody></table></div></div></div></div><div class='getfile-btn' style='margin-top:0'><button type='button' class='btn btn-default btn-sm pull-left' onclick='CreateFolder()'>"+lan.bt.adddir+"</button><button type='button' class='btn btn-danger btn-sm mr5' onclick=\"layer.close(getCookie('ChangePath'))\">"+lan.public.close+"</button> <button type='button' id='bt_select' class='btn btn-success btn-sm' >"+lan.bt.path_ok+"</button></div>"
                      });
                      _this.set_cookie('ChangePath', loadT.form);
                  setTimeout(function(){		
                    $('#btn_back').click(function(){
                      var path = $("#PathPlace").find("span").text();			
                      path = bt.rtrim(bt.format_path(path),'/');						
                      var back_path = bt.get_file_path(path);
                      
                      get_file_list(back_path);
                    })	
                    //选择
                    $('#bt_select').click(function(){						
                      var path = bt.format_path($("#PathPlace").find("span").text());
                              path = bt.rtrim(path, '/');
                      // $("#"+id).val(path);
                      // $("."+id).val(path);
                      rf_id=id.split('_')
                      _this[rf_id[0]][rf_id[1]]=path;
                      loadT.close();
                    })
                  },100)
                  get_file_list($("#" + id).val())
                  
                  function get_file_list(path)
                      {
                          bt.send('GetDir', 'files/GetDir', { path: path, disk: true }, function (rdata) {
                              var d = '',a='';                
                      if(rdata.DISK != undefined) {
                        for(var f = 0; f < rdata.DISK.length; f++) {
                          a += "<dd class=\"bt_open_dir\" path =\""+rdata.DISK[f].path+"\"><span class='glyphicon glyphicon-hdd'></span>&nbsp;" + rdata.DISK[f].path + "</dd>"
                        }
                        $("#changecomlist").html(a)
                      }
                      for(var f = 0; f < rdata.DIR.length; f++) {
                        var g = rdata.DIR[f].split(";");
                        var e = g[0];
                        if(e.length > 20) {
                          e = e.substring(0, 20) + "..."
                        }
                        if(isChineseChar(e)) {
                          if(e.length > 10) {
                            e = e.substring(0, 10) + "..."
                          }
                        }
                        d += "<tr><td class=\"bt_open_dir\" path =\"" + rdata.PATH + "/" + g[0] + "\"  title='" + g[0] + "'><span class='glyphicon glyphicon-folder-open'></span>" + e + "</td><td>" + bt.format_data(g[2]) + "</td><td>" + g[3] + "</td><td>" + g[4] + "</td><td><span class='delfile-btn' onclick=\"NewDelFile('" + rdata.PATH + "/" + g[0] + "')\">X</span></td></tr>"
                      }
                      
                      if(rdata.FILES != null && rdata.FILES != "") {
                        for(var f = 0; f < rdata.FILES.length; f++) {
                          var g = rdata.FILES[f].split(";");
                          var e = g[0];
                          if(e.length > 20) {
                            e = e.substring(0, 20) + "..."
                          }
                          if(isChineseChar(e)) {
                            if(e.length > 10) {
                              e = e.substring(0, 10) + "..."
                            }
                          }
                          d += "<tr><td title='" + g[0] + "'><span class='glyphicon glyphicon-file'></span>" + e + "</td><td>" + bt.format_data(g[2]) + "</td><td>" + g[3] + "</td><td>" + g[4] + "</td><td></td></tr>"
                        }
                      }
                      
                      $(".default").hide();
                      $(".file-list").show();
                      $("#tbody").html(d);
                      if(rdata.PATH.substr(rdata.PATH.length - 1, 1) != "/") {
                        rdata.PATH += "/"
                      }
                      $("#PathPlace").find("span").html(rdata.PATH);
                      
                      $('.bt_open_dir').click(function(){	
                        get_file_list($(this).attr('path'));
                      })
                    })
                      }

                      function ActiveDisk() {
                          var a = $("#PathPlace").find("span").text().substring(0, 1);
                          switch (a) {
                              case "C":
                                  $(".path-con-left dd:nth-of-type(1)").css("background", "#eee").siblings().removeAttr("style");
                                  break;
                              case "D":
                                  $(".path-con-left dd:nth-of-type(2)").css("background", "#eee").siblings().removeAttr("style");
                                  break;
                              case "E":
                                  $(".path-con-left dd:nth-of-type(3)").css("background", "#eee").siblings().removeAttr("style");
                                  break;
                              case "F":
                                  $(".path-con-left dd:nth-of-type(4)").css("background", "#eee").siblings().removeAttr("style");
                                  break;
                              case "G":
                                  $(".path-con-left dd:nth-of-type(5)").css("background", "#eee").siblings().removeAttr("style");
                                  break;
                              case "H":
                                  $(".path-con-left dd:nth-of-type(6)").css("background", "#eee").siblings().removeAttr("style");
                                  break;
                              default:
                                  $(".path-con-left dd").removeAttr("style")
                          }
                      }
                },


            }

        })
    })
</script>
